package com.wc.算法提高课.F第六章_基础算法.递推与递归.费解的开关;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/1 10:38
 * @description https://www.acwing.com/problem/content/97/
 */
public class Main {
    /**
     * 思路：只要第一行确定了, 其他所有行都确定了
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 5;
    static char[][] g = new char[N][N], bg = new char[N][];
    static int[] dx = {-1, 1, 0, 0, 0}, dy = {0, 0, -1, 1, 0};

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            for (int i = 0; i < N; i++) bg[i] = sc.next().toCharArray();
            int res = 10;
            for (int op = 0; op < 32; op++) {
                for (int i = 0; i < N; i++) {
                    System.arraycopy(bg[i], 0, g[i], 0, N);
                }
                int cnt = 0;
                // 操作第一行的灯
                for (int i = 0; i < N; i++) {
                    if ((op >> i & 1) == 1) {
                        turn(0, i);
                        cnt++;
                    }
                }
                // 操作2 ~ 4行的灯
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < N; j++) {
                        if (g[i][j] == '0') {
                            turn(i + 1, j);
                            cnt++;
                        }
                    }
                }
                boolean success = true; 
                // 检查最后一行的灯是否全部亮着
                for (int i = 0; i < N; i++) {
                    if (g[4][i] == '0') {
                        success = false;
                        break;
                    }
                }
                if (success && cnt < res) res = cnt;
            }
            if (res <= 6) out.println(res);
            else out.println(-1);
        }
        out.flush();
    }

    static void turn(int x, int y) {
        for (int i = 0; i < 5; i++) {
            int a = x + dx[i], b = y + dy[i];
            if (a < 0 || a >= N || b < 0 || b >= N) continue;
            g[a][b] ^= 1;
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
